package com.bsoft.gol.hcs.clinical.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.TreatmentAcupointsDetailEntity;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class TreatmentAcupointsDetailEntityDAO extends HibernateSupportDelegateDAO<TreatmentAcupointsDetailEntity> {

    @DAOMethod(sql = "delete from TreatmentAcupointsDetailEntity where treatmentAcupointsId =:treatmentAcupointsId")
    public abstract void deleteByAcupointsId(@DAOParam("treatmentAcupointsId") String treatmentAcupointsId);

    public List<TreatmentAcupointsDetailEntity> findByTreatmentId(final String treatmentId){

       HibernateStatelessResultAction<List<TreatmentAcupointsDetailEntity>> action = new AbstractHibernateStatelessResultAction<List<TreatmentAcupointsDetailEntity>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(" select tad.treatment_acupoints_detail_id treatmentAcupointsDetailId ,tad.treatment_acupoints_id treatmentAcupointsId," +
                        " tad.meridian_id meridianId,tad.meridian_name meridianName ,tad.acupoint_id acupointId ,tad.acupoint_name  acupointName," +
                        " tad.acupoint_part_code acupointPartCode ,tad.acupoint_part_name acupointPartName ,tad.acupoint_method_code acupointMethodCode ," +
                        " tad.acupoint_method_name  acupointMethodName ,tad.continue_time continueTime, tad.needling_depth needlingDepth ," +
                        " tad.acupoint_type acupointType,tad.remark ,ta.tenant_id tenantId ,tad.organization_id organizationId,tad.organization_name organizationName " +
                        " from treatment_acupoints ta,treatment_acupoints_detail tad " +
                        " where ta.treatment_acupoints_id=tad.treatment_acupoints_id  ");
                if (StringUtils.isNotEmpty(treatmentId)){
                    stringBuilder.append(" and ta.treatment_id =:treatmentId ");
                }
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuilder.toString());
                sqlQuery.setParameter("treatmentId",treatmentId);
                sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map> list = sqlQuery.list();
                List<TreatmentAcupointsDetailEntity> data= new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)){
                    for (Map map:list) {
                        TreatmentAcupointsDetailEntity convert = ConversionUtils.convert(map, TreatmentAcupointsDetailEntity.class);
                        data.add(convert);
                    }
                }
                setResult(data);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
